local LauncherLayer = class("LauncherLayer", require "widget/BaseUI")
 
 
LauncherLayer.GameInfoStruct = {
    m_nGameId = 0,
    m_szPackageName = nil,
    m_szDownload = nil,
    m_szVersion = nil,
    szGameIntro = nil,
    m_nStartOrder = 0
}

LauncherLayer.m_items = {}
LauncherLayer.m_btnGamesPoint = {}
LauncherLayer.m_GameIntroSprite = {}


function LauncherLayer.show()
    print("============LauncherLayer.show===============")
 
    local win = LauncherLayer.new()

    local data = getWndInitParamList()
    data.isRemoveResource = false
    data.imgTitle = nil
    data.bgImage = nil
    data.isShowCover = false,

    win:init(data)
    win:showWindow()

    return win

end

function LauncherLayer:ctor() 
    self.super:ctor();
    SpriteFrameMgr:getInstance():addSpriteFrameFile("ui/launcher/gamepic0.plist")
    SpriteFrameMgr:getInstance():addSpriteFrameFile("ui/launcher/launcher0.plist")
    
    self.m_Angle = 0 
    self.m_iGameCount = 0 
    self.m_iCurrentBtn = 1 
    self.m_GameIntroSprite = {}

end


function LauncherLayer:init(data)
    self:initWithParas(data)
    
    self.m_Json = ccs.GUIReader:getInstance():widgetFromJsonFile("ui/launcher/launcher.Json")
    self.m_Json:setPositionX(-WinSize.width/2)
    self.m_Json:setPositionY(-WinSize.height/2) 
    self:setContentSize(self.m_Json:getContentSize())
    local WinSize = cc.Director:getInstance():getWinSize()
    self.m_Json:setScaleX(WinSize.width/self.m_Json:getContentSize().width)
    self.m_Json:setScaleY(WinSize.height/self.m_Json:getContentSize().height)
    self:addChild(self.m_Json)
   
    self.m_PanelMain = ccui.Helper:seekWidgetByName(self.m_Json,"PanelMain")
 
    self.m_labelID = ccui.Helper:seekWidgetByName(self.m_PanelMain,"Main_labelID")
    self.m_LabelNick = ccui.Helper:seekWidgetByName(self.m_PanelMain,"Main_LabelNick")
   
    
    self.m_BtnUpdatePwd = ccui.Helper:seekWidgetByName(self.m_PanelMain,"Main_BtnUpdatePwd")
    self.m_BtnUpdatePwd:setTouchEnabled(true)
    self.m_BtnUpdatePwd:addTouchEventListener(function(sender,eventType)self:onMenutClick(sender,eventType) end)
    
    self.m_BtnBack = ccui.Helper:seekWidgetByName(self.m_PanelMain,"Main_BtnBack")
    self.m_BtnBack:setTouchEnabled(true)
    self.m_BtnBack:addTouchEventListener(function(sender,eventType)self:onMenutClick(sender,eventType) end)

    self.m_BtnSound = ccui.Helper:seekWidgetByName(self.m_PanelMain,"Main_BtnSound")
    self.m_BtnSound:setTouchEnabled(true)
    self.m_BtnSound:addTouchEventListener(function(sender,eventType)self:onMenutClick(sender,eventType) end)
    
    self.m_BtnAbout = ccui.Helper:seekWidgetByName(self.m_PanelMain,"Main_BtnAbout")
    self.m_BtnAbout:setTouchEnabled(true)
    self.m_BtnAbout:addTouchEventListener(function(sender,eventType)self:onMenutClick(sender,eventType) end)
    
    self.m_BtnNotice = ccui.Helper:seekWidgetByName(self.m_PanelMain,"Main_BtnNotice")
    self.m_BtnNotice:setTouchEnabled(true)
    self.m_BtnNotice:addTouchEventListener(function(sender,eventType)self:onMenutClick(sender,eventType) end)
    
    self.m_BtnShare = ccui.Helper:seekWidgetByName(self.m_PanelMain,"Main_BtnShare")
    self.m_BtnShare:setTouchEnabled(true)
    self.m_BtnShare:addTouchEventListener(function(sender,eventType)self:onMenutClick(sender,eventType) end)
    
  
 
    self.m_ImageViewAbstract = ccui.Helper:seekWidgetByName(self.m_PanelMain,"Main_ImageViewAbstract")
     
    self.m_ImageView = ccui.ImageView:create()
    self.m_ImageView:setAnchorPoint(0, -0.5)
    self.m_ImageView:setPositionX(30)
    self.m_ImageView:setPositionY(0) 
    self.m_ImageView:setContentSize(cc.size(394,157))
    self.m_ImageView:loadTexture("ui/gamepic/gameInfo_1.png", ccui.TextureResType.plistType)
    self.m_ImageViewAbstract:addChild(self.m_ImageView)
 
    self.m_box = require("widget/BaseLayout"):create(true)
    self.m_scrollView = ccui.ScrollView:create()
    self.m_scrollView:setLocalZOrder(0)
    self.m_scrollView:setAnchorPoint(0, 0)
 --   self.m_scrollView:setBackGroundColor(cc.c3b(0,255,0))
 --   self.m_scrollView:setBackGroundColorType(ccui.LayoutBackGroundColorType.solid)
    local WinSize = cc.Director:getInstance():getWinSize()
    self.m_scrollView:setContentSize(cc.size(WinSize.width/2+70, 300))
    self.m_scrollView:setPositionX(0)
    self.m_scrollView:setPositionY(380)
    self.m_scrollView:setDirection(ccui.ScrollViewDir.vertical)
    self.m_scrollView:setTouchEnabled(true)
    self.m_scrollView:setInertiaScrollEnabled(true)
    self.m_scrollView:addEventListener(handler(self, self.scrollEventCallback))
    self.m_scrollView:setBounceEnabled(true)
    self.m_scrollView:addChild(self.m_box)
    self.m_PanelMain:addChild(self.m_scrollView)
    
 
    local userInfo = ds.Account:getUserLoginInfo()
    self.m_labelID:setText(userInfo.llUserID)
    self.m_LabelNick:setText(userInfo.szNickName)
    self:setGameInfoButton()
    
    local listener = cc.EventListenerTouchOneByOne:create()
    listener:registerScriptHandler(handler(self, self.onTouchBegan), cc.Handler.EVENT_TOUCH_BEGAN)
    listener:registerScriptHandler(handler(self, self.onTouchMoved), cc.Handler.EVENT_TOUCH_MOVED)
    listener:registerScriptHandler(handler(self, self.onTouchEnded), cc.Handler.EVENT_TOUCH_ENDED)
    listener:setSwallowTouches(self.m_isSwollowTouch)
    local eventDispatcher = self:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)
    
end

function LauncherLayer:scrollEventCallback(obj, type)
    if type == ccui.ScrollviewEventType.scrolling then
    end
end

function LauncherLayer:setGameInfoButton()
    removeAllChild(self.m_box)
    local list =  ds.Account:getGameList()
    local count = #list -2

    for i = 1, count do
        local cell = require("view/Launcher/GameIcon"):create()
        cell:setHander(self)
        cell:setName("cell" .. i)
        cell:setItemData(list[i])
        self.m_box:addChild(cell)
    end
    self.m_box:layoutWithGrid(2, #list/2, 2, 2)
    self.m_box:setAnchorPoint(cc.p(0, 0))
    self.m_box:setPosition(cc.p(-50, -70))
  
    self.m_scrollView:setInnerContainerSize(cc.size(self.m_box:getContentSize().width, self.m_box:getContentSize().height))
end



function LauncherLayer:onTouchBegan(touch, event)
 
    return true
end

function LauncherLayer:onTouchMoved(touch, event)
   
            
end

function LauncherLayer:onTouchEnded(touch, event)
    
end

function LauncherLayer:onMenutClick(sender,eventType)
    if eventType == ccui.TouchEventType.ended then
        if sender == self.m_BtnUpdatePwd then     
            print "=========LauncherLayer:onMenutClick m_BtnUpdatePwd======================="
            require "view/Launcher/UpdatePwdLayer".show(self)
        elseif sender == self.m_BtnBack then     
            print "=========LauncherLayer:onMenutClick m_BtnBack======================="
            self:closeWindow()
        elseif sender == self.m_BtnSound then     
            print "=========LauncherLayer:onMenutClick m_BtnSound======================="
        elseif sender == self.m_BtnAbout then     
            print "=========LauncherLayer:onMenutClick m_BtnAbout======================="
            require "view/Launcher/AboutLayer".show(self)
        elseif sender == self.m_BtnNotice then     
            print "=========LauncherLayer:onMenutClick m_BtnNotice======================="
            require "view/Launcher/NoticeLayer".show(self)
        elseif sender == self.m_BtnShare then     
            print "=========LauncherLayer:onMenutClick m_BtnShare======================="
            require "view/Launcher/ShareLayer".show(self)
        end
    end
end
 
return LauncherLayer